#!/bin/sh
# ------------------------------------------------------------------------
# FDSProxy start with console
# ------------------------------------------------------------------------

# Not all shells support the 'exec -a newname' syntax..
`exec -a test echo test 2> /dev/null`
if [ "$?" -eq "1" ] ; then
  EXEC="exec -a fdsproxy"
else
  # Lets switch to bash if you have it installed...
  if [ -f "/bin/bash" ] ; then
    exec "/bin/bash" $0 $@ 
  else
    EXEC="exec"
  fi
fi

# OS specific support.
cygwin=false;
darwin=false;
case "`uname`" in
  CYGWIN*) cygwin=true ;;
  Darwin*) darwin=true
           if [ -z "$JAVA_HOME" ] ; then
             JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
           fi
           ;;
esac

if [ -z "$FDSPROXY_HOME" ] ; then

  ## resolve links - $0 may be a link to fdsproxy's home
  PRG="$0"

  # need this for relative symlinks
  dirname_prg=`dirname "$PRG"`

  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '.*/.*' > /dev/null; then
    PRG="$link"
    else
    PRG=`dirname "$PRG"`"/$link"
    fi
  done

  FDSPROXY_HOME=`dirname "$PRG"`
  cd "$FDSPROXY_HOME"
fi

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
  [ -n "$FDSPROXY_HOME" ] &&
    FDSPROXY_HOME=`cygpath --unix "$FDSPROXY_HOME"`
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
  [ -n "$CLASSPATH" ] &&
    CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi

if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD=`which java 2> /dev/null `
    if [ -z "$JAVACMD" ] ; then
        JAVACMD=java
    fi
  fi
fi

if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly."
  echo "  We cannot execute $JAVACMD"
  exit 1
fi

CLASSPATH="${FDSPROXY_HOME}/FDSProxy-1.1.0.jar"

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  FDSPROXY_HOME=`cygpath --windows "$FDSPROXY_HOME"`
  JAVA_HOME=`cygpath --windows "$JAVA_HOME"`
  CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
  CYGHOME=`cygpath --windows "$HOME"`
fi

if [ -z "$JVM_FLAGS" ] ; then
  JVM_FLAGS="-server -Xmx1G -Xss8M -XX:MaxPermSize=1024m -XX:ReservedCodeCacheSize=150m -XX:+AggressiveOpts -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+CMSClassUnloadingEnabled -XX:SurvivorRatio=128 -XX:MaxTenuringThreshold=0"
fi

# Attempt to bump up the ulimit 
ulimit -n 100000 > /dev/null 2> /dev/null


${EXEC} "$JAVACMD" ${JVM_FLAGS} -classpath "${CLASSPATH}" aims.app.FDSProxy $@
